package models

import (
	"github.com/astaxie/beego/orm"

	"gopkg.in/gomail.v2"
	"fmt"
)
//发送表
type Send struct {
	Id                 int64
	Cmpname            string
	Cmptel             string
	Cmpmail            string
	Sendtime           string
	Errtimes           string
	Status             string

}
//建立模型
func init() {
	orm.RegisterModel(new(Send))
}

//查询发送详情
func QuerySend() (send []orm.Params) {
	o := orm.NewOrm()
	Send := new(Send)
	qs := o.QueryTable(Send).Filter("Status", "0")
	qs.Values(&send)
	return send
}
//修改发送配置
func SendEdit(g *Send) (int64, error) {
	o := orm.NewOrm()
	send := make(orm.Params)
	//sendId:=g.Id
	send["Cmpname"] = g.Cmpname
	send["Cmptel"] = g.Cmptel
	send["Cmpmail"] = g.Cmpmail
	send["Sendtime"] = g.Sendtime
	send["Errtimes"] = g.Errtimes
	k := gomail.NewMessage()
	k.SetHeader("From", "a751891786@sina.com")
	//m.SetHeader("To", "eson.fan@cnfc.cc")
	k.SetHeader("To", g.Cmpmail)
	k.SetHeader("Subject","测试啊!")
	k.SetBody("text/html", "你好！<b>"+g.Cmpname+"</b>!")
	//m.Attach("/home/Alex/lolcat.jpg")
	d := gomail.NewDialer("smtp.sina.com", 25, "a751891786@sina.com", "nimade110")
	if err := d.DialAndSend(k); err != nil {
		panic(err)
	}
	var table Send
	num, err := o.QueryTable(table).Filter("Id", 1).Update(send)
	return num, err

}


//直接sql 关联查询
func Test111() (maps []orm.Params) {
	o := orm.NewOrm()
	o.Raw("SELECT * FROM   entry e ,entourage ea  WHERE e.id=ea.Entry_Id AND e.id=?", 1).Values(&maps)
	fmt.Println("TestSql...................",maps)
	return maps
}